package cn.johnyu.jmsdemo;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;


public class PtpConsumerAsyn {

	public static void main(String[] args) throws Exception{
		ConnectionFactory factory=new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
		Destination queue=new ActiveMQQueue("john01");
		Connection con=factory.createConnection();
		con.start();
		Session sen=con.createSession(false, Session.AUTO_ACKNOWLEDGE);
		MessageConsumer consumer=sen.createConsumer(queue);
//		new Thread(){
//			public void run() {
//				TextMessage msg=(TextMessage) consumer.receive();
//			};
//		}.start();
		consumer.setMessageListener(new MessageListener() {
			
			public void onMessage(Message msg) {
				TextMessage tmsg=(TextMessage)msg;
				try {
					System.out.println(tmsg.getText());
				} catch (JMSException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
		System.out.println("out....");
		Thread.sleep(30000);
		sen.close();
		con.close();
		
		

	}

}
